home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / FALCON / ACC / DRIVERS.ACC / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-10  |  7.7 KB  |  327 lines

  1. /* ======================================================================
  2.  * FILE: FILEIO.C
  3.  * ======================================================================
  4.  * DATE: November 23, 1992
  5.  *       January 20, 1993
  6.  *
  7.  * This file handles the I/O for the options dialog box which 
  8.  * allows the user to change the parameters of a driver.
  9.  */
  10.  
  11.  
  12. /* INCLUDE FILES
  13.  * ======================================================================
  14.  */
  15. #include <sys\gemskel.h>
  16. #include <string.h>
  17. #include <tos.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. #include "country.h"
  22. #include "drvhead.h"
  23. #include "mainstuf.h"
  24. #include "text.h"
  25. #include "fsmio.h"
  26. #include "options.h"
  27. #include "drivers.h"
  28.  
  29.  
  30.  
  31. /* PROTOTYPES
  32.  * ======================================================================
  33.  */
  34. BOOLEAN     Read_Data( void );
  35. int     *FindString( int *ptr, int *endptr );
  36. BOOLEAN  Save_Data( void );
  37. int     CountDPI( HEADER *hdptr );
  38. int     SearchDPI( int xvalue, int yvalue );
  39.  
  40.  
  41. /* DEFINES
  42.  * ======================================================================
  43.  */
  44. #define E_OK         0
  45.  
  46.  
  47. /* EXTERNALS
  48.  * ======================================================================
  49.  */
  50.  
  51.  
  52. /* GLOBALS
  53.  * ======================================================================
  54.  */
  55. char    FPath[ 128 ];        /* Directory path */
  56. int    xdpi[4];
  57. int    ydpi[4];
  58. int     icount;
  59. long    mbuffer;
  60. int  *DataPtr;
  61. long EndIndex;
  62.  
  63.  
  64. /* FUNCTIONS
  65.  * ======================================================================
  66.  */
  67.  
  68.  
  69. /* Read_Data()
  70.  * ================================================================
  71.  * Read in the data and keep the buffer till later.
  72.  */
  73. BOOLEAN
  74. Read_Data( void )
  75. {
  76.    DTA  thedta, *saved;
  77.    int  fd;
  78.    BOOLEAN flag;
  79.    
  80.             
  81.    flag = FALSE;      
  82.    saved = Fgetdta();
  83.    Fsetdta( &thedta );
  84.    if( Fsfirst( FPath, 0 ) <= -1 ) /* Can't find the file... */
  85.    {                   
  86.      form_alert( 1, alert52 );
  87.    }
  88.    else
  89.    {
  90.      DataBuf = calloc( 1, thedta.d_length * 2L );
  91.  
  92.      if( DataBuf )
  93.      {
  94.        fd = Fopen( FPath, 0 );    
  95.        if( fd <= 0 )
  96.        {
  97.          form_alert( 1, alert51 );
  98.        }
  99.        else
  100.        {
  101.          Buffer = ( long )DataBuf;
  102.          Buffer = ( Buffer + 15L ) & 0xFFFFFFF0L;
  103.          DataHdr = ( int *)Buffer;
  104.          
  105.          Fread( fd, thedta.d_length, DataHdr );
  106.          Fclose( fd );
  107.      
  108.          EndIndex = thedta.d_length;
  109.          DataPtr  = FindString( DataHdr, DataHdr + EndIndex - 8 );
  110.          if( DataPtr )
  111.          {
  112.            hdr = ( HEADER *)DataPtr;
  113.            hdr_buffer = ( HEADER )*hdr;
  114.  
  115.            strcpy( title, hdr->fname );
  116.        icount = CountDPI( hdr );
  117.  
  118.        mbuffer = ( long )&hdr->TopMargin;
  119.  
  120.        if( icount >= 3 )    /* Get's us past a third page table */
  121.           mbuffer += 16L;
  122.        
  123.        if( icount >= 4 )    /* Get's us past a fourth page table */
  124.           mbuffer += 16L;
  125.           
  126.        mhdr = ( MARGIN *)mbuffer;
  127.            flag = TRUE;
  128.          }
  129.          else
  130.          {
  131.            form_alert( 1, alert53 );  
  132.          }
  133.        }  
  134.      }
  135.      else
  136.        form_alert( 1, alert54 );  
  137.    }
  138.    Fsetdta( saved );    
  139.    return( flag );
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /* FindString()
  146.  * ======================================================================
  147.  */
  148. int
  149. *FindString( int *ptr, int *endptr )
  150. {
  151.     long *xptr;
  152.     
  153.     while( ptr < endptr )
  154.     {
  155.        /* Look for _FSM_HDR */
  156.        xptr = (long *)ptr;
  157.        if( ((long)*xptr == 0x5f46534dL ) &&
  158.            ((long)*(xptr+1) == 0x5f484452L ) )
  159.        return( ptr );
  160.        ptr++;
  161.     }   
  162.     return( ( int *)NULL );
  163. }
  164.  
  165.  
  166. /* Save_Data()
  167.  * ======================================================================
  168.  * Save out the Data - Note- The buffer is still there from the read_data.
  169.  */
  170. BOOLEAN
  171. Save_Data( void )
  172. {
  173.    DTA  thedta, *saved;
  174.    int  fd;
  175.    BOOLEAN flag;
  176.    int  index;
  177.                
  178.    flag = FALSE;      
  179.    saved = Fgetdta();
  180.    Fsetdta( &thedta );
  181.    if( Fsfirst( FPath, 0 ) <= -1 ) /* Can't find the file... */
  182.    {                   
  183.      form_alert( 1, alert52 );
  184.    }
  185.    else
  186.    {
  187.      if( DataBuf )
  188.      {
  189.          if( DataPtr )
  190.          {
  191.            hdr_buffer.quality    = Menu[ MQUALITY ].curvalue;
  192.            hdr_buffer.nplanes    = Menu[ MCOLOR ].curvalue + 1;
  193.            hdr_buffer.PageSize   = Menu[ MPAGESIZE ].curvalue;
  194.            hdr_buffer.xres       = xres_value;
  195.            hdr_buffer.yres       = yres_value;
  196.            hdr_buffer.port       = Menu[ MPORT ].curvalue;
  197.            hdr_buffer.paper_feed = Menu[ MTRAY ].curvalue;
  198.  
  199.  
  200.            *hdr = hdr_buffer;
  201.        /* NEED TO FIGURE OUT WHERE TOPMARGIN GOES...*/
  202.        index = SearchDPI( hdr->X_PIXEL[ Menu[ MHSIZE ].curvalue ], 
  203.                      hdr->Y_PIXEL[ Menu[ MVSIZE ].curvalue ] );
  204.        
  205.        /* We're NOW at TopMargin!!!*/       
  206.  
  207.        /* check if Page Area Offset is ON or OFF */
  208.        if( IsSelected( AREAON ) )
  209.        {
  210.            /* It's ON! - look at page table margins */
  211.         /* Take the number from the appropriate page margin
  212.          * and shove it into the TopMargin etc...
  213.          */
  214.            mhdr->TopMargin    = mhdr->Margin[ ( index * 4 ) + 0 ];
  215.            mhdr->BottomMargin = mhdr->Margin[ ( index * 4 ) + 1 ];
  216.            mhdr->LeftMargin   = mhdr->Margin[ ( index * 4 ) + 2 ];
  217.            mhdr->RightMargin  = mhdr->Margin[ ( index * 4 ) + 3 ];
  218.        }
  219.        else
  220.        {
  221.            /* It's OFF! - Shove zeros into it.*/
  222.            mhdr->TopMargin    = 0;
  223.            mhdr->BottomMargin = 0;
  224.            mhdr->LeftMargin   = 0;
  225.            mhdr->RightMargin  = 0;
  226.        }           
  227.            flag = TRUE;
  228.  
  229.            fd = Fcreate( FPath, 0 );
  230.            if( fd >= 0 )
  231.            {
  232.              Fwrite( fd, EndIndex, DataHdr );
  233.              Fclose( fd );
  234.            }
  235.            else
  236.              form_alert( 1, alert51 );  
  237.          }
  238.          else
  239.          {
  240.            form_alert( 1, alert53 );  
  241.          }  
  242.      }
  243.      else
  244.        form_alert( 1, alert54 );  
  245.    }
  246.    Fsetdta( saved );    
  247.    return( flag );
  248. }
  249.  
  250.  
  251.  
  252. /* CountDPI()
  253.  * ======================================================================
  254.  */
  255. int
  256. CountDPI( HEADER *hdptr )
  257. {
  258.     int  count;
  259.     int  i,j;
  260.     int  curx, cury;
  261.     
  262.     for( i = 0; i < 4; i++ )
  263.     {
  264.        xdpi[i] = ydpi[i] = 0;
  265.     }
  266.  
  267.     count = 0;    
  268.     if( hdptr )
  269.     {
  270.     curx = cury = 0;       
  271.         for( i = 0; i < hdr->total_planes; i++ )
  272.         {
  273.            curx = hdr->X_PIXEL[i];
  274.            cury = hdr->Y_PIXEL[i];
  275.            
  276.            if( curx && cury )
  277.            {
  278.                for( j = 0; j < 4; j++ )
  279.                {
  280.                  /* The slot is open! shove one in...*/
  281.                  if( !xdpi[j] && !ydpi[j] )
  282.                  {
  283.                     count++;
  284.                     xdpi[j] = curx;
  285.                     ydpi[j] = cury;
  286.                     break;
  287.                  }
  288.                  
  289.                  /* This one already exists...*/
  290.                  if( ( curx == xdpi[j] ) && ( cury == ydpi[j] ) )
  291.                     break;
  292.                }
  293.                /* if we reach here without finding an empty slot...
  294.                 * or matching...
  295.                 * we're in trouble, cause I don't know how to handle
  296.                 * more than 4 different dpi's.
  297.                 */
  298.            }
  299.         }        
  300.     }
  301.     if( !count || ( count > 4 ))
  302.        count = 1;
  303.     return( count );
  304. }
  305.  
  306.  
  307.  
  308. /* Find the matching xdpi and ydpi in our array */
  309. int
  310. SearchDPI( int xvalue, int yvalue )
  311. {
  312.      int index;
  313.      
  314.      for( index = 0; index < 4; index++ )
  315.      {
  316.      if( !xdpi[index ] && !ydpi[index ] )
  317.         return( 0 );
  318.         
  319.          if( ( xdpi[index] == xvalue ) && ( ydpi[index] == yvalue ) )
  320.          {
  321.             return( index );
  322.          }
  323.          
  324.      }
  325.      return( 0 );
  326. }
  327.